<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-tabs type="border-card" v-model="videoName" @tab-change="changeTab">
			<el-tab-pane label="抓拍照片" name="image">
				<el-image :src="liveImage" style="width:100%"></el-image>
			</el-tab-pane>
			<el-tab-pane label="实时视频" name="video">
				<sc-live v-if="videoInfo.liveUrl" :src="videoInfo.liveUrl" :isLive="true"
					:autoplay="true" ref="video"></sc-live>
				<div v-if="!videoInfo.liveUrl" class="noVideo">
					<div class="noVideoBox">
						<noVideo />
					</div>
				</div>
			</el-tab-pane>
		</el-tabs>
		<div class="u-m-t-15"></div>
		<el-card shadow="never">
			<el-row :gutter="0">
				<el-col :lg="12">
					<el-form-item label="门店名称" prop="name">
						{{FormData.storeName}}
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="设备/通道" prop="name">
						{{FormData.deviceId}}
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="告警信息" prop="name">
						{{FormData.alarmTypeName}}
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="告警时间" prop="name">
						{{FormData.alarmTime}}
					</el-form-item>
				</el-col>
			</el-row>

		</el-card>

		<template #footer>
			<el-button @click="visible=false">消警</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">通知负责人</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		noVideo
	} from '@/assets/icons'
	import scLive from '@/components/scVideo/live'
	import nopic from '@/assets/images/nopic.png'
	export default {
		emits: ['success', 'closed'],
		components: {
			scLive,
			noVideo
		},
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				channelList: [],
				//表单数据
				FormData: {},
				videoInfo: {},
				videoName: 'image',
				liveImage: nopic
			}
		},
		created() {

		},
		mounted() {},
		methods: {
			async changeTab(e) {
				console.log(e)
				if (e === 'video') {
					let sdata = {
						id: this.FormData.deviceId,
						protocol: 'HTTP_FLV',
						channel: [this.FormData.channelId],
					}
					const res = await this.$API.api.cam.live.post(sdata)
					console.log(res)
					if (res.code == 200) {
						this.videoInfo.liveUrl = res.data[0].liveUrl
					}
				}

			},
			async getInfo() {
				let data = {
					alarmUuid: this.FormData.alarmUuid,
					channelId: this.FormData.channelId,
					deviceId: this.FormData.deviceId
				}
				const res = await this.$API.api.alert.image.post(data)
				if (res.code == 200) {
					this.liveImage = res.data
				}
				console.log(res)
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true
				this.channelList.forEach(item => {
					if (item.name == '') {
						this.$message('请输入通道名称')
						return
					}
					if (item.tag == '') {
						this.$message('请输入通道别名')
						return
					}
				})
				delete this.FormData.createByName
				console.log(this.FormData)
				let data = {
					store: {
						id: this.FormData.store.id
					},
					unit: {
						id: this.FormData.unit.id
					},
					name: this.FormData.name,
					sn: this.FormData.sn,
					code: this.FormData.code,
					functions: this.FormData.functions,
					devStatus: this.FormData.devStatus
				}
				if (this.FormData.id) {
					data.id = this.FormData.id
				}
				if (this.channelList.length) {
					data.channels = this.channelList
				}
				const res = await this.$API.api.cam.save.post(data)
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.FormData, data)
				console.log(data)
				this.getInfo()
			}
		}
	}
</script>

<style>
</style>
